package com.gxar.quick.ar.server.db.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.utils.PageUtils;
import com.gxar.quick.ar.server.base.common.enums.ExchangeItemGiftEnum;
import com.gxar.quick.ar.server.base.model.exchange.dto.ExchangeItemTransferTypeFrequencyStatDTO;
import com.gxar.quick.ar.server.base.model.exchange.mapper.ExchangeItemRecordMapstruct;
import com.gxar.quick.ar.server.base.model.exchange.vo.response.AdminExchangeItemStatisticsResponse;
import com.gxar.quick.ar.server.base.model.exchange.vo.response.AdminExchangeItemTransferPageResponse;
import com.gxar.quick.ar.server.base.model.exchange.vo.response.AdminExchangeItemTransferTopPageResponse;
import com.gxar.quick.ar.server.db.mapper.ExchangeItemTransferRecordMapper;
import com.gxar.quick.ar.server.db.model.ExchangeItemTransferRecordEntity;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/2/29 16:37
 */
@Service
@RequiredArgsConstructor
public class ExchangeItemRecordService {

    private final ExchangeItemTransferRecordMapper transferRecordMapper;

    /**
     * 赠送转赠记录分页查询
     *
     * @param userProfile 用户信息
     * @param pageNum     当前页
     * @param pageSize    页记录数
     * @param type        类型
     * @return 响应
     */
    public Result<PageResult<AdminExchangeItemTransferPageResponse>> transferRecordPage(UserProfile userProfile, int pageNum, int pageSize, Integer type) {
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<ExchangeItemTransferRecordEntity> entityList = transferRecordMapper.queryByPage(type);
        if (CollectionUtils.isEmpty(entityList)) {
            return Result.success(new PageResult<>());
        }
        List<AdminExchangeItemTransferPageResponse> pageResponseList = ExchangeItemRecordMapstruct.INSTANCE.entitiesToPageResponses(entityList);
        PageInfo<AdminExchangeItemTransferPageResponse> responsePageInfo = new PageInfo<>(pageResponseList);
        responsePageInfo.setPageNum(pageNum);
        responsePageInfo.setTotal(page.getTotal());
        return PageUtils.getPageResult(responsePageInfo, pageResponseList);
    }

    /**
     * 转赠统计
     *
     * @param userProfile 用户信息
     * @return 统计响应
     */
    public AdminExchangeItemStatisticsResponse transferStatistics(UserProfile userProfile) {
        // 收款总额
        BigDecimal amountCollected = transferRecordMapper.totalAmountCollected(ExchangeItemGiftEnum.HANDSEL.getCode());
        // 兑换品总数
        Integer totalExchangeItemNum = transferRecordMapper.totalExchangeItemNum();
        // 次数
        List<ExchangeItemTransferTypeFrequencyStatDTO> statDto = transferRecordMapper.typeTransferFrequencyStat();
        Map<Integer, ExchangeItemTransferTypeFrequencyStatDTO> statDtoMap = Optional.ofNullable(statDto).orElse(Collections.emptyList()).stream().collect(Collectors.toMap(ExchangeItemTransferTypeFrequencyStatDTO::getType, t -> t, (t1, t2) -> t1));
        AdminExchangeItemStatisticsResponse response = new AdminExchangeItemStatisticsResponse();
        response.setTotalAmountCollected(Objects.isNull(amountCollected) ? BigDecimal.ZERO : amountCollected);
        response.setTransferNum(Objects.isNull(totalExchangeItemNum) ? 0 : totalExchangeItemNum);
        response.setTransferFrequency(Objects.isNull(statDtoMap.get(ExchangeItemGiftEnum.HANDSEL.getCode())) ? 0 : statDtoMap.get(ExchangeItemGiftEnum.HANDSEL.getCode()).getFrequency());
        response.setSendFrequency(Objects.isNull(statDtoMap.get(ExchangeItemGiftEnum.HANDSEL.getCode())) ? 0 : statDtoMap.get(ExchangeItemGiftEnum.HANDSEL.getCode()).getFrequency());
        return response;
    }

    /**
     * 兑换品排行榜
     *
     * @param userProfile 用户信息
     * @param pageNum     当前页
     * @param pageSize    页记录数
     * @param type        类型
     * @return 响应
     */
    public Result<PageResult<AdminExchangeItemTransferTopPageResponse>> transferRecordTopList(UserProfile userProfile, int pageNum, int pageSize, Integer type) {
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<AdminExchangeItemTransferTopPageResponse> entityList = transferRecordMapper.topList(type);
        if (CollectionUtils.isEmpty(entityList)) {
            return Result.success(new PageResult<>());
        }
        PageInfo<AdminExchangeItemTransferTopPageResponse> responsePageInfo = new PageInfo<>(entityList);
        responsePageInfo.setPageNum(pageNum);
        responsePageInfo.setTotal(page.getTotal());
        return PageUtils.getPageResult(responsePageInfo, entityList);
    }
}
